package com.liuzh.fitnessplan.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.os.Looper
import android.preference.Preference
import android.util.Log
import android.widget.Toast
import androidx.room.Room
import com.liuzh.fitnessplan.R
import com.liuzh.fitnessplan.base.BaseActivity
import com.liuzh.fitnessplan.database.Database
import com.liuzh.fitnessplan.database.bean.Sports
import com.liuzh.fitnessplan.database.bean.User
import com.liuzh.fitnessplan.database.dao.UserDao
import com.liuzh.fitnessplan.databinding.ActivityLoginBinding
import com.liuzh.fitnessplan.tools.ActivitysManager
import com.liuzh.fitnessplan.tools.RoomDatabaseUtil

class LoginActivity : BaseActivity() {
    private lateinit var bind : ActivityLoginBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bind = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(bind.root)
        val db = RoomDatabaseUtil.instance.getDatabase(this)

        setOnClick(db)

    }
    private fun setOnClick(db : Database){
        bind.loginBtn.setOnClickListener {

            val userAccount : Int = if( bind.accountName.text.toString() == ""){
                Toast.makeText(this,"账号不能为空",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }else{
                bind.accountName.text.toString().toInt()
            }
            val userpasswd : String = if( bind.accountPasswd.text.toString() == ""){
                Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }else{
                bind.accountPasswd.text.toString()
            }

            Thread{
                var loginRequestValue = db.userDao().login(userAccount, userpasswd).size
                var all = db.userDao().getAll()
                Log.d("LoginThread","$loginRequestValue")
                if(loginRequestValue != 1){
                    Looper.prepare()
                    Toast.makeText(this,"账号或密码错误",Toast.LENGTH_SHORT).show()
                    Log.d("LoginThread","$loginRequestValue")
                    Looper.loop()
                }else{
                    val intent = Intent(this,MainActivity::class.java)
                    loginInfo(userAccount,true)

                    startActivity(intent)
                    db.close()
                    this.finish()
                }
            }.start()
        }

        bind.textGoToRegister.setOnClickListener{startActivity(Intent(this,RegisterActivity::class.java))}
    }



    @SuppressLint("CommitPrefEdits")
    private fun loginInfo(userName : Int, isLogin : Boolean){
        val sharedPreferences = getSharedPreferences("appInfo", Context.MODE_PRIVATE)
        val spEdit = sharedPreferences.edit()
        spEdit.putInt("userId",userName)
        spEdit.putBoolean("isLogin", isLogin)
        spEdit.apply()
    }
}
